<?php
if(!defined('IN')) exit('Access Denied');
/*
	function list
	bool fheader()	//网站头部
	bool ffooter()	//网站尾部
*/

//如果第二个参数不写，则返回上一页
function alert($info, $url='')
{
	$str = '<meta http-equiv="Content-Type" content="text/html; charset=gbk"><script type="text/javascript">alert("'.$info.'");window.location.href="';
	$str .= ($url=='') ? 'javascript:history.back()' : $url;
	$str .= '";</script>';
	echo $str;
}

//alert('3455','http://www.chinapet.com');


function noalert ($url='')
{
	$str = '<meta http-equiv="Content-Type" content="text/html; charset=gbk"><script type="text/javascript">window.location.href="';
	$str .= ($url=='') ? 'javascript:history.back()' : $url;
	$str .= '";</script>';
	echo $str;
}


/*
*  -------------------------------------------------
*   自定义MD5加密函数
*  -------------------------------------------------
*/
function licuicui_md5 ($password,$pre_str='licuicui',$next_str='cms_08_#741')
{
	return md5($pre_str.$password.$next_str);   //fdsjkfjdsfsadmincms_08_#741
}

//echo licuicui_md5('pEKuGd5ed7Ea').'<br/>';    //5584e929a6a8b092d65dfc8fdb7ba142
//echo  md5('pEKuGd5ed7Ea');					  //5584e929a6a8b092d65dfc8fdb7ba142
/*
*  -------------------------------------------------
*   后台登录验证函数
*  -------------------------------------------------
*/
function checklogin()
{
	 $get_user=getCookie('q_cpc_admin');
	 if(empty($get_user))
	 {
		  echo "<script>window.location.href='index.php?act=login';</script>";
	 }
}

/*
*  -------------------------------------------------
*   用户登录验证函数
*  -------------------------------------------------
*/
function check_user_login ()
{
	if(empty($_SESSION['user']['tmd']['username']))
	{
		echo "<script>window.location.href='user.php?act=login';</script>";
	}
}

/*
*  -------------------------------------------------
*   简单分页函数
*   flag==1,首页  上一页 下一页  末页;flag==2   << 1 2 3 4 5 >>
*  -------------------------------------------------
*/
function licuicui_page ($totalrows,$url,$pagesize=10,$var='page',$flag=1)
{
	$totalpages = ceil($totalrows/$pagesize);
	$page = isset ( $_GET[$var] ) ? intval ( $_GET[$var] ) : 1 ;
	$page = ($page < 1) ? 1 : $page ;
//	$page = ($page > $totalpages) ? $totalpages : $page ;
	$page = (($page > $totalpages) && ($totalpages>1)) ? $totalpages : $page ;
	$to = (stripos($url,'?')===false) ? '?' : '&' ;
	$pl = "";
	if($totalpages > 1)
	{
		if($flag==1)
		{
			$pl .="<a href='{$url}'>首页</a>";
			if($page > 1)
			{
				$pl .= "&nbsp;<a href='{$url}{$to}{$var}=".($page-1)."'>上一页</a>";
			}
			if($page < $totalpages)
			{
				$pl .= "&nbsp;<a href='{$url}{$to}{$var}=".($page+1)."'>下一页</a>";
			}
			$pl .= "&nbsp;<a href='{$url}{$to}{$var}={$totalpages}'>末页</a>";
			$pl .="&nbsp;&nbsp;当前第{$page}页/共{$totalpages}页&nbsp;";
			$pl .= "<select onChange=\"window.location.href='".$url.$to.$var.'=\'+this.value'."\"; > ";
			for ($i=1;$i<=$totalpages;$i++)
			{
				$pl .="<option value='{$i}'";
				if($i == $page)
				{
					$pl .= " selected";
				}
				$pl .= ">第{$i}页</option>";
			}
			$pl .= "</select>";
		}
		else if ($flag==2)
		{
			$pl .= "<a href='{$url}'><<</a>&nbsp";
			$for_start = $page-5;
			if($for_start < 1)
			{
				$for_start = 1;
			}
			$for_end = $page+5;
			if($for_end > $totalpages )
			{
				$for_end = $totalpages;
			}
			for ($i=$for_start;$i<=$for_end;$i++)
			{
				if($i == $page)
				{
					$pl.=$i.'&nbsp';
				}
				else
				{
					$pl .= "<a href='{$url}{$to}{$var}={$i}'>{$i}</a>&nbsp";
				}
			}
			$pl .= "<a href='{$url}{$to}{$var}={$totalpages}'>>></a>&nbsp;&nbsp;";
			$pl .= "<select onChange=\"window.location.href='".$url.$to.$var.'=\'+this.value'."\"; > ";
			for ($i=1;$i<=$totalpages;$i++)
			{
				$pl .="<option value='{$i}'";
				if($i == $page)
				{
					$pl .= " selected";
				}
				$pl .= ">{$i}</option>";
			}
			$pl .= "</select>";
		}
	}
	$start = ($page-1)*$pagesize;
	return $pl=array('pl'=>$pl,'start'=>$start,'pagesize'=>$pagesize);
}


/*
*  -------------------------------------------------
*   图片上传函数
*   licuicui_upload ($photo,500,$time_post,$_dir='cms')
*  -------------------------------------------------
*/
	function Image_upload ($photo,$time_post,$_dir='',$size='1024')
	{
		/*
		[name] => 648191eeg99a54e9990fc&690.jpg
		[type] => image/jpeg
		[tmp_name] => C:\Windows\Temp\phpB16.tmp
		[error] => 0
		[size] => 60094
		*/
		if($photo['error']) exit(alert('图片上传出错'));

		//判断图片大类   image
//		echo $photo['type'];
//		echo '<br />';
//		echo strtolower(substr($photo['type'],0,5));
//		exit;
		if(strtolower(substr($photo['type'],0,5))!='image') exit(alert('只能上传图片'));

		//判断图片具体类型
		$type = array('jpg','png','jpeg','gif','x-png','pjpeg');
		if(!in_array(strtolower(substr($photo['type'],6)),$type)) exit(alert('只能上传jpg、png或gif图'));


		$photo_name_detail = pathinfo($photo['name']);    //[name] => 1f3c4c99641d3c1e6e068c29.jpg
		$ext = $photo_name_detail['extension'];    //文件扩展名

		if(!in_array(strtolower($ext),$type)) exit(alert('只能上传jpg、png或gif图'));


		if($photo['size'] > $size*1024) exit(alert('图片大小不能超过'.$size.'k'));


		$dir = realpath(ROOT)."/images/".$_dir;//upload/brand  upload/goods
		if(!is_dir($dir)) mkdir($dir,0777);

		if(!is_dir($dir.'/'.date('Ymd',$time_post))) mkdir($dir.'/'.date('Ymd',$time_post),0777);  //upload/brand/20121221

		//文件命名
		$photo_name = date('YmdHis',$time_post).rand(111111,999999).'.'.$ext;      //2012 12 21 08 08 08

		//$photo['tmp_name'] => C:\Windows\Temp\php45EF.tmp
		if(move_uploaded_file($photo['tmp_name'],$dir.'/'.date('Ymd',$time_post).'/'.$photo_name))
		{
			return $photo_name;
		}
		else
		{
			return false;
		}
	}

	/*
	*  -------------------------------------------------
	*one 为接收的数组 ,dir为要删除的的目录(只删除文件,不删除目录)
	*  -------------------------------------------------
	*/
	function delImage ($one,$_dir='')
	{
		$dir = realpath(ROOT)."/images/".$_dir.'/'.date('Ymd',$one['time_post']).'/'.$one['album_name'];
		if(is_file($dir)) unlink($dir);

		$dir = realpath(ROOT)."/images/".$_dir.'/'.date('Ymd',$one['time_post']).'/s_'.$one['album_name'];
		if(is_file($dir)) unlink($dir);

		$dir =realpath(ROOT)."/images/".$_dir.'/'.date('Ymd',$one['time_post']).'/m_'.$one['album_name'];
		if(is_file($dir)) unlink($dir);
	}

	/*
	*  -------------------------------------------------
	*   递归删除目录以及目录下文件
	*  -------------------------------------------------
	*/
	//循环删除目录和文件函数
	function delDirAndFile( $dirName )
	{
			if ( $handle = opendir( "$dirName" ) )
			{
			   while ( false !== ( $item = readdir( $handle ) ) )
			   {
				   if ( $item != "." && $item != ".." )
				   {
					   if ( is_dir( "$dirName/$item" ) )
					   {
							delDirAndFile( "$dirName/$item" );
					   }
					   else
					   {
							if( unlink( "$dirName/$item" ) )echo "成功删除文件： $dirName/$item<br />\n";
					   }
				   }
			   }
		   closedir( $handle );
		   if( rmdir( $dirName ) )echo "成功删除目录： $dirName<br />\n";
		}
	}

/*
*  -------------------------------------------------
*   递归分类(根据id去查子id)
*  -------------------------------------------------
*/
   function licuicui_sort ($table,$parent_id=0,$t=-1)
   {
	static $_lcc = array();
	$t++;
	$sql = "SELECT * FROM `{$table}` WHERE `parent_id`='{$parent_id}' ORDER BY `time_post` DESC";
	$query = $GLOBALS['db']->fetch_all($sql);
		if($query)
		{
			foreach ($query as $key => $val )
			{
				if($val['parent_id']!=0)
				{
					$val['sort_name'] = str_repeat('&nbsp;&nbsp;',$t*4)."┣".$val['sort_name'];
				}else
				{
					$val['sort_name']=trim($val['sort_name']);
				}
				$_lcc[] = $val;
				licuicui_sort($table,$val['id'],$t);
			}
			return $_lcc;
		}
   }

   /*
   *  -------------------------------------------------
   *   获取子分类 (获取id)
   *  -------------------------------------------------
   */
	function licuicui_childs($table,$id)
	{
		static $str='';
		$sql="SELECT * FROM `{$table}` WHERE `parent_id`='{$id}'";
		$query = $GLOBALS['db']->fetch_all($sql);
		if($query)
		{
			foreach ($query as $key => $val )
			{
				$str .=','.$val['id'];
				licuicui_childs($table,$val['id']);
			}
		}
       return $str=ltrim($str,',');
	}

		/*
	*  -------------------------------------------------
	*   调特殊物品(道具 优惠卷 勋章)
	*  -------------------------------------------------
	*/
	function getSpecialGoods($table,$dir,$where,$num=6)
	{
		$data=array();
	  	$sql="SELECT * FROM `{$table}` WHERE {$where} LIMIT {$num} ";
		$data=$GLOBALS['db']->fetch_all($sql);
		if($data)
		{
			foreach ($data as $key => $val )
			{
					if($val['photo'])
					{
						$data[$key]['s_imgs']='http://q.chinapet.com/upload/'.$dir.'/'.$val['photo'];
					}
					else
					{
						$data[$key]['s_imgs']='http://q.chinapet.com/upload/nopic.gif';
					}
			}
		}
		return $data;
	}
